package model

import (
	"homepage_srv/homepage"
	"smart-health-platforms/config"
	"time"
)

type SearchLogs struct {
	Id         int64     `gorm:"column:id;type:bigint;primaryKey;not null;" json:"id"`
	UserId     uint64    `gorm:"column:user_id;type:bigint UNSIGNED;comment:用户id;default:NULL;" json:"user_id"`                   // 用户id
	SearchTerm string    `gorm:"column:search_term;type:varchar(255);comment:搜索词;" json:"search_term"`                            // 搜索词
	Hit        uint64    `gorm:"column:hit;type:bigint UNSIGNED;comment:点击量;default:NULL;" json:"hit"`                            // 点击量
	CreatedAt  time.Time `gorm:"column:created_at;type:datetime(3);comment:添加时间;default:CURRENT_TIMESTAMP(3);" json:"created_at"` // 添加时间
}

func (s *SearchLogs) TableName() string {
	return "search_logs"
}

func (s *SearchLogs) Creat() error {
	return config.DB.Create(&s).Error
}
func (s *SearchLogs) List(name string) error {
	return config.DB.Where("search_term = ?", name).Find(&s).Error
}

// 增加点击量
func (s *SearchLogs) IncreaseClickThroughRates(name string) error {
	return config.DB.Model(&s).Where("search_term = ?", name).Update("hit", s.Hit+1).Error
}

// 查询热搜词
func (s *SearchLogs) ListHotSearch() (list []*homepage.GoodHotSearch, err error) {
	err = config.DB.Model(&s).Order("hit desc").Find(&list).Error
	return
}

// 查询单条热搜词
func (s *SearchLogs) ListOneHotSearch() error {
	return config.DB.Where("id = ?", s.Id).Find(&s).Error
}
